package com.dzf.controller;

import com.dzf.entity.ItemBook;
import com.dzf.entity.Order;
import com.dzf.entity.RespJson;
import com.dzf.service.OrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/adm/ord")
public class AdmOrdController {
    @Autowired
    OrdService ordService;

    @RequestMapping("/addShip")
    public RespJson addShip(Order ord){
        ordService.addShip(ord);
        return RespJson.pack(200,"OK",null);
    }

    @RequestMapping("/findOrdInfo/{start}/{num}")
    public RespJson findOrdInfo(@RequestBody Map<String,Object> map, @PathVariable("start") int start, @PathVariable("num")int num)
    {
        //计算该页从第几行开始
        map.put("start",(start*num)-num);
        map.put("num",num);
        Map<String,Object> resultmap =  ordService.findOrdInfo(map);
        return RespJson.pack(200,"OK",resultmap);
    }

    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    public RespJson findItemInfoByOrdid(@PathVariable("ordid") int ordid){
        List<ItemBook> itemBooks = ordService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200,"OK",itemBooks);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

